home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 10
/
FM Towns Free Software Collection 10.iso
/
ms_dos
/
tool
/
txf
/
src
/
txfbuf.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-09-17
|
4KB
|
155 lines
/*====================================================================
*
* TXF file buffering module
*
*====================================================================
* copyright(C) 1992-1994 T.Nakatani
*====================================================================
*/
#include "txf.h"
char far *buf_base;
char far *bufrp;
long int bufremain, bufyet, bufldval, bufoffset, buffsize;
char far *buffileopen(int i)
{
char *tmpinname, *tmpoutname;
struct stat sbuf;
int rd;
tmpinname = ((tmpinfile == -1) ? inputfile : tfile[tmpinfile]);
tmpoutname = tfile[((tmpinfile > 0) ? 0 : 1)];
if (stat(tmpinname, &sbuf) == 0) {
buffsize = fsize = sbuf.st_size;
}
else {
buffsize = fsize = 0;
}
if (*tmpinname != NUL) {
inputh = open(tmpinname, O_RDONLY | O_BINARY);
}
else {
errexit("Cannot open inputfile.");
}
outputh = open(tmpoutname, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, S_IWRITE);
#ifdef DEBUG
if (viewmode > 2) {
fprintf(stderr, "Info:read=%s(%d),write=%s(%d),tmpinfile=%d\n",
tmpinname, inputh, tmpoutname, outputh, tmpinfile);
}
#endif
if ((inputh == -1) || (outputh == -1)) {
fprintf(stderr, "Error:cannot open TMP file(%d)\n", i);
#ifdef DEBUG
fprintf(stderr, "Info:read=%s(%d),write=%s(%d)\n",
tmpinname, inputh, tmpoutname, outputh);
#endif
exit(1);
}
if ((buf_base = farmalloc(rbufsize+2)) == NULL) {
rbufsize /= 2;
if ((buf_base = farmalloc(rbufsize+2)) == NULL) {
errexit("out of memory.");
}
}
bufremain = bufyet = buffsize;
bufldval = min(rbufsize-1, bufyet);
bufoffset = 0;
if (_dos_read(inputh, buf_base, bufldval, &rd) != 0) {
errexit("read error");
}
bufyet -= rd;
bufrp = NULL;
return (buf_base);
}
char far *readnext(int val)
{
unsigned int rd;
if (bufrp == NULL) {
bufrp = buf_base;
if (val > 1) {
bufrp += (long)val-1;
}
bufremain -= val;
}
else {
bufrp += (long)val;
bufoffset += val;
bufremain -= val;
if ((bufoffset > rbufsize-2048) && (bufyet > 0)) {
far_memcpy(buf_base, buf_base+(long)(rbufsize-4096), 4095);
bufldval = min(rbufsize-4096, bufyet);
if (_dos_read(inputh, buf_base+(long)4095, bufldval, &rd) != 0) {
errexit("read error");
}
bufyet -= rd;
bufoffset -= rbufsize-4096;
bufrp = buf_base + (long)bufoffset;
}
}
return (bufrp);
}
#if 0
char *readbefore(int val)
{
bufrp -= val;
bufoffset -= val;
bufremain += val;
if (bufoffset < 0) {
if (fseek(input, -(rbufsize>>1), SEEK_CUR) != 0) {
fseek(input, 0, SEEK_SET);
bufyet = buffsize;
bufldval = min(rbufsize-1, bufyet);
bufyet -= (unsigned long)fread(buf_base, 1, bufldval, input);
bufoffset = buffsize - bufremain;
bufrp = buf_base + bufoffset;
}
else {
bufyet += ((rbufsize>>1)+bufldval);
bufldval = min(rbufsize-1, bufyet);
bufyet -= fread(buf_base, 1, bufldval, input);
bufoffset += (rbufsize>>1);
bufrp = buf_base + bufoffset;
}
}
return (bufrp);
}
#endif
long int remainbyte()
{
return ((bufremain < 0) ? 0 : bufremain);
}
void bufclose()
{
close(inputh);
close(outputh);
farfree(buf_base);
tmpinfile = ((tmpinfile > 0) ? 0 : 1);
#ifdef DEBUG
if (viewmode > 2) {
fprintf(stderr, "Info:tmpinfile = %d\n",tmpinfile);
}
#endif
}
int chktouch(char far *rptr, int iy)
{
if (ishflg) {
if (ishchk(rptr) > 0) return (1);
}
if (quoteflg) {
if (quotechk(rptr)) {
return (1);
}
}
if (levelchk(iy)) {
return (1);
}
return (0);
}